package edu.zhku.ttms.view.panel.content;

import edu.zhku.ttms.domain.Ticket;
import edu.zhku.ttms.service.TicketService;
import edu.zhku.ttms.service.impl.TicketServiceImpl;
import edu.zhku.ttms.utils.LoginSession;
import edu.zhku.ttms.utils.SystemConst;
import edu.zhku.ttms.view.ViewController;
import edu.zhku.ttms.view.button.*;
import edu.zhku.ttms.view.dialog.TicketAddDialog;
import edu.zhku.ttms.view.dialog.TicketModDialog;

import javax.swing.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 林志强
 * @since 2021/4/24 13:16
 */
public class TicketManagementPanel extends AbstractManagementPanel {

    static TicketService ticketService = new TicketServiceImpl();

    @Override
    public void loadData(String queryStr, int page) {

        this.setVisible(false);

        dataPanel.removeAll();

        dataPanel.setBounds(100, 70, 960, 540);

        String[][] columnNames = {{"序号", "30"}, {"车牌号", "100"}, {"驾驶证号", "100"}, {"违章时间", "130"}, {"违章地点", "140"}, {"罚款金额", "70"}, {"缴费状态", "70"}};

        List<Ticket> list = ticketService.getList(queryStr, LoginSession.getLoggedPoliceId(), null);
        Object[][] data = new Object[list.size()][columnNames.length];
        for (int i = 0; i < list.size(); i++) {
            Ticket ticket = list.get(i);
            String licensePlateNumber = ticket.getCar().getLicensePlateNumber();
            String driverLicenseNumber = ticket.getDriverLicenseNumber();
            Date violationTime = ticket.getViolationTime();
            String violationTimeStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(violationTime);
            String violationPlace = ticket.getViolationPlace();
            BigDecimal amount = ticket.getAmount();
            Integer status = ticket.getStatus();
            String statusStr = status == 0 ? "未缴费" : "已缴费";
            data[i] = new Object[]{i + 1, licensePlateNumber, driverLicenseNumber, violationTimeStr, violationPlace, amount, statusStr};
        }

        int x = 0;
        int y = 0;

        int width = 150;
        int height = 30;
        int buttonWidth = 70;
        int buttonHeight = 25;

        int totalRow = data.length;

        int fromIndex = SystemConst.PAGE_SIZE * (page - 1);
        int toIndex = Math.min(SystemConst.PAGE_SIZE * page, totalRow);

        int labelX = x;
        for (String[] columnName : columnNames) {
            int labelWidth = Integer.parseInt(columnName[1]);
            JLabel jLabel = new JLabel(columnName[0]);
            jLabel.setBounds(labelX, y, labelWidth, height);
            dataPanel.add(jLabel);
            labelX += labelWidth;
        }

        for (int i = 0; i < toIndex - fromIndex; i++) {
            labelX = x;
            for (int j = 0; j < data[0].length; j++) {
                int labelWidth = Integer.parseInt(columnNames[j][1]);
                JLabel jLabel = new JLabel(data[fromIndex + i][j].toString());
                jLabel.setBounds(labelX, y + height + i * height, labelWidth, height);
                dataPanel.add(jLabel);
                labelX += labelWidth;
            }
            Ticket ticket = list.get(i + fromIndex);
            Long ticketId = ticket.getTicketId();

            JButton modifyButton = new ModifyButton();
            modifyButton.addActionListener(e -> new TicketModDialog(ticket).setVisible(true));
            modifyButton.setBounds(labelX, y + height + i * height, buttonWidth, buttonHeight);
            dataPanel.add(modifyButton);

            JButton deleteButton = new DeleteButton();
            deleteButton.addActionListener(e -> {
                int deleteConfirm = JOptionPane.showConfirmDialog(this, "确定要删除该罚单信息？", "确认信息", JOptionPane.YES_NO_OPTION);
                if (deleteConfirm == JOptionPane.YES_OPTION) {
                    ticketService.removeById(ticketId);
                    ViewController.changeView(new TicketManagementPanel());
                }
            });
            deleteButton.setBounds(labelX + buttonWidth + 10, y + height + i * height, buttonWidth, buttonHeight);
            dataPanel.add(deleteButton);

            if (ticket.getStatus() == 1) {
                modifyButton.setEnabled(false);
                deleteButton.setEnabled(false);
            }
        }

        JButton addTicketButton = new AddButton();
        addTicketButton.setBounds(780, 40, buttonWidth, buttonHeight);
        addTicketButton.addActionListener(e -> new TicketAddDialog().setVisible(true));
        this.add(addTicketButton);

        int pageY = y + (SystemConst.PAGE_SIZE + 1) * height;
        int totalPage = Math.max((int)Math.ceil((double)totalRow / SystemConst.PAGE_SIZE), 1);
        JLabel pageInfoLabel = new JLabel("第" + page + "页，共" + totalPage + "页");
        pageInfoLabel.setBounds(x, pageY, width, buttonHeight);
        dataPanel.add(pageInfoLabel);

        boolean hasPrePageButton = page > 1;
        boolean hasNextPageButton = toIndex < totalRow;
        if (hasPrePageButton) {
            JButton prePageButton = new PrePageButton();
            prePageButton.addActionListener(e -> loadData(queryStr, page - 1));
            prePageButton.setBounds(x + width, pageY, buttonWidth, buttonHeight);
            dataPanel.add(prePageButton);
        }
        if (hasNextPageButton) {
            JButton nextPageButton = new NextPageButton();
            nextPageButton.addActionListener(e -> loadData(queryStr, page + 1));
            nextPageButton.setBounds(hasPrePageButton ? x + 2 * width : x + width, pageY, buttonWidth, buttonHeight);
            dataPanel.add(nextPageButton);
        }

        this.add(dataPanel);
        this.setVisible(true);
    }
}
